define([
  'text!' + ELMP.resource('components/selectProject/index.html', 'lib')
],
  /**
   * @returns {typeof import('types/vue/types/index').default}
   */
  function (template) {
    let list = []
    return {
      template,
      name: 'SelectProject',
      model: {
        prop: 'value',
        event: 'change'
      },
      props: {
        value: {
          type: Object
        }
      },
      data() {
        return {
          project: null,
          options: [],
          loading: false
        }
      },
      watch: {
        value: {
          handler() {
            this.handleValue()
          },
          immediate: true
        }
      },
      created() {
        this.getdata()
      },
      methods: {
        handleValue() {
          this.project = this.value
        },
        async getdata() {
          const res = await $.elApi.project.queryProjectsExt({
            page_size: 1000,
            page_index: 1
          })
          const list = res.res.data.records
          const userList = res.res.users

          this.options = list.map(item => {
            const user = userList.find(user => user.id === item.pmId)
            item.user = user
            return item
          })
        },
        change(data) {
          this.$emit('change', data)
        },
      }
    }
  })
